package com.pro.control;

import com.pro.entity.JsonData;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RequestMapping("/upload")
@Controller
public class UploadController {

    private String name = null;
    @RequestMapping(value="/upload")
    @ResponseBody
    public JsonData upload(HttpServletRequest request, HttpSession session, MultipartFile[] fil) throws IllegalStateException, IOException {
        List<String> list = new ArrayList<>();
        for (MultipartFile multipartFile : fil) {
            String end = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."));
            String path = null;
            Date date = new Date();
            if(multipartFile.getSize() > 0) {
                name = date.getTime()+end;
                path = System.getProperty("user.dir")+"/src/main/resources/static/images";
                File file = new File(path,name);
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
            }
            list.add(path+"/"+name);
        }
        return list.get(0) != null?JsonData.buildSuccess(list.get(0)):JsonData.buildError("服务器错误");
    }

    @RequestMapping(value="/uploadCourseVideo")
    @ResponseBody
    public JsonData uploadVideo(HttpServletRequest request, HttpSession session, MultipartFile[] fil) throws IllegalStateException, IOException {
        List<String> list = new ArrayList<>();
        for (MultipartFile multipartFile : fil) {
            String end = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."));
            String path = null;
            Date date = new Date();
            if(multipartFile.getSize() > 0) {
                name = date.getTime()+end;
                path = System.getProperty("user.dir")+"/src/main/resources/static/images/course/video";
                File file = new File(path,name);
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
            }
            list.add(path+"/"+name);
        }
        return list.get(0) != null?JsonData.buildSuccess(list.get(0)):JsonData.buildError("服务器错误");
    }

    @RequestMapping(value="/uploadCourseImg")
    @ResponseBody
    public JsonData uploadCourseImg(HttpServletRequest request, HttpSession session, MultipartFile[] fil) throws IllegalStateException, IOException {
        List<String> list = new ArrayList<>();
        for (MultipartFile multipartFile : fil) {
            String end = multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."));
            String path = null;
            Date date = new Date();
            if(multipartFile.getSize() > 0) {
                name = date.getTime()+end;
                path = System.getProperty("user.dir")+"/src/main/resources/static/images/course/img";
                File file = new File(path,name);
                FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
            }
            list.add(path+"/"+name);
        }
        return list.get(0) != null?JsonData.buildSuccess(list.get(0)):JsonData.buildError("服务器错误");
    }





}
